package com.zeei.ems.base.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import com.zeei.ems.base.constant.Constatnt;
import com.zeei.ems.base.service.IWaterStatisticService;
import com.zeei.ems.base.vo.DataQueryVO;
import com.zeei.ems.base.vo.GradesCollVo;
import com.zeei.ems.base.vo.WaterGradesVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class WaterJudegUtil {

	@Autowired
	IWaterStatisticService waterStatisticService;

	// 静态初使化 一个工具类 这样是为了在spring初使化之前
	private static WaterJudegUtil waterJudegUtil;

	private static List<WaterGradesVO> gradesList = new ArrayList<>();

	private static List<WaterGradesVO> lankGradesList = new ArrayList<>();

	public static final Map<String, WaterGradesVO> gradesMap = new HashMap<>();

	// 湖泊水质
	public static final Map<String, WaterGradesVO> lankGradesMap = new HashMap<>();

	// 仪器设备告警,监测仪器故障时的数据,仪器设备处于校准状态,仪器跨度检查,仪器零点检查,仪器停止运行
	// 该段时间内无测量值,仪器精度检查,仪器零点校准,仪器跨度校准,仪器维护期间的数据,仪器与数采仪通讯故障
	@SuppressWarnings("unchecked")
	public static final List<String> otherFlag = Arrays
			.asList(new String[] { "B", "BA", "C", "PS", "PZ", "Rao", "RM", "AS", "CZ", "CS", "D", "BB" });

	// 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
//	@PostConstruct
	public void init() {
		waterJudegUtil = this;
		gradesList = waterJudegUtil.waterStatisticService.getWaterDatas();
		// 获取对应的评判标准
		for (WaterGradesVO grade : gradesList) {

			gradesMap.put(grade.getPolluteCode(), grade);

		}
		lankGradesList = waterJudegUtil.waterStatisticService.queryWaterGrades("[secType1]");

		for (WaterGradesVO grade : lankGradesList) {

			lankGradesMap.put(grade.getPolluteCode(), grade);
		}

	}

	/**
	 * getLevelCode:获取对应的污染等级 注意:如果resultMap中包含secType字段的值,则认为为湖泊类型,则使用湖泊标准范围
	 * @param resultMap
	 * @return int
	 */
	public static int getLevelCode(Map<String, Double> resultMap) {
		int resultCode = 0;
		int flag;
		Double auditValue=null;
		WaterGradesVO waterGrade;
		// 默认为断面
		Map<String, WaterGradesVO> data = gradesMap;
		if (resultMap.containsKey("secType")) {
			data = lankGradesMap;
			resultMap.remove("secType");
		}
		// 遍历结果值，与对应的评判标准对比得出最终水质等级
		for (Entry<String, Double> entry : resultMap.entrySet()) {

			Double audit = entry.getValue();
			// 对应的虚拟因子 PH值为0不参与水质评价 2020-2
			String polluteCode = entry.getKey();
			if(StringUtil.isNotEmpty(polluteCode) && audit!=null) {
				if (Constatnt.PH.equals(polluteCode) && audit!=0) {

					waterGrade = data.get(entry.getKey());
					if(waterGrade==null) {
						continue;
					}
					if (audit > waterGrade.getFive() || audit < waterGrade.getOne()) {

						resultCode = 6;
						break;
					} else {
						resultCode = resultCode < 1 ? 1 : resultCode;
						continue;
					}
				}

				if (data.containsKey(entry.getKey())) {

					waterGrade = data.get(entry.getKey());

					flag = waterGrade.getOrderTy();
					auditValue = audit * flag;
					if (waterGrade.getFive() != null && auditValue > waterGrade.getFive() * flag) {

						resultCode = 6;
						break;
					} else if (waterGrade.getOne() != null && auditValue <= waterGrade.getOne() * flag) {

						resultCode = resultCode < 1 ? 1 : resultCode;
					} else if (waterGrade.getThree() != null && auditValue <= waterGrade.getThree() * flag) {

						if (waterGrade.getTwo() != null && auditValue <= waterGrade.getTwo() * flag) {

							resultCode = resultCode < 2 ? 2 : resultCode;
						} else {
							resultCode = resultCode < 3 ? 3 : resultCode;
						}
					} else {

						if (waterGrade.getFour() != null && auditValue <= waterGrade.getFour() * flag) {

							resultCode = resultCode < 4 ? 4 : resultCode;
						} else {
							resultCode = resultCode < 5 ? 5 : resultCode;
						}
					}

				}
			}



		}
		return resultCode;

	}

	/**
	 * gradesToGra:判定指标集合转化
	 *
	 * @param grades
	 * @return List<GradesCollVo>
	 */
	public static List<WaterGradesVO> gradesToGra(List<GradesCollVo> grades) {

		Map<String, WaterGradesVO> resMap = new HashMap<>();

		if (!CollectionUtils.isEmpty(grades)) {
			for (GradesCollVo grade : grades) {
				WaterGradesVO waterGrades = new WaterGradesVO();
				// 没有就新建
				if (resMap.containsKey(grade.getPolluteCode())) {
					waterGrades = resMap.get(grade.getPolluteCode());
				} else {
					waterGrades = new WaterGradesVO();
					// 相同指标的这些值是相同的，设置一次就可以
					waterGrades.setJudge(grade.getIsJudge());
					waterGrades.setOrderTy(grade.getOrderNum());
					waterGrades.setType(grade.getPolluteName());
					waterGrades.setPolluteCode(grade.getPolluteCode());
					waterGrades.setPolluteName(grade.getPolluteName());
				}
				// ph特殊处理
				if (Constatnt.PH.equals(grade.getPolluteCode())) {
					waterGrades.setFive(grade.getsMaxValue());
					waterGrades.setOne(grade.getsMinValue());
					resMap.put(grade.getPolluteCode(), waterGrades);
					continue;
				}
				if (StringUtil.isNotEmpty(grade.getEnvLevel())) {
					switch (grade.getEnvLevel()) {
						case "1":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setOne(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setOne(grade.getsMinValue());
							}
							break;
						case "2":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setTwo(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setTwo(grade.getsMinValue());
							}
							break;
						case "3":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setThree(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setThree(grade.getsMinValue());
							}
							break;
						case "4":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setFour(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setFour(grade.getsMinValue());
							}
							break;
						case "5":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setFive(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setFive(grade.getsMinValue());
							}
							break;
						case "7":
							// 正序排列
							if (grade.getOrderNum() != null && grade.getOrderNum() > 0) {
								waterGrades.setThree(grade.getsMaxValue());
							} else {// 反序排列
								waterGrades.setThree(grade.getsMinValue());
							}
							break;
						default:
							break;
					}
				}

				resMap.put(grade.getPolluteCode(), waterGrades);
			}

		}
		return new ArrayList<WaterGradesVO>(resMap.values());

	}
}
